package com.xxx.userservice.service;

import com.xxx.userservice.pojo.entity.User;
import com.baomidou.mybatisplus.extension.service.IService;
import io.seata.rm.tcc.api.BusinessActionContext;
import io.seata.rm.tcc.api.BusinessActionContextParameter;
import io.seata.rm.tcc.api.LocalTCC;
import io.seata.rm.tcc.api.TwoPhaseBusinessAction;

import java.math.BigDecimal;

/**
 * <p>
 *  服务类
 * </p>
 *
 * @author guwq
 * @since 2025-05-22
 */
@LocalTCC   // seata TCC模式
public interface UserService extends IService<User> {

    User getByUserId(int userId);

    BigDecimal getMoney(String userId);

    // seata TCC模式：定义Try、Confirm、Cancel，其中业务侧负责调用Try，Confirm和Cancel由Seata框架来调用
    // useTCCFence = true表示使用Seata提供的功能帮助处理TCC模式的幂等性、空回滚、悬挂资源问题
    @TwoPhaseBusinessAction(name = "prepareDeduct", commitMethod = "confirm", rollbackMethod = "cancel", useTCCFence = true)
    boolean prepareDeduct(@BusinessActionContextParameter(paramName = "userId") String userId,
                          @BusinessActionContextParameter(paramName = "amount") BigDecimal amount);

    boolean confirm(BusinessActionContext context);

    boolean cancel(BusinessActionContext context);
}
